<?php
include($_SERVER['DOCUMENT_ROOT']."/inc/Config.php");
include($_SERVER['DOCUMENT_ROOT']."/inc/Mysql_class.php");
include($_SERVER['DOCUMENT_ROOT']."/inc/Define.php");
error_reporting(0);
$db = new db($conf);
$cplx = isset($_GET['cplx'])?intval($_GET['cplx']):0;
$save_path = "/upload/excel/";

if(!isset($_FILES)){
	echo "<script>alert('未获取到上传文件');window.location.href='goodslist.php';</script>";
	exit;
}
$cf = explode(".",$_FILES['file']['name']);
$houzhui = $cf[count($cf)-1];
$name = MD5(time());

if($houzhui!="xlsx"){ 
	echo "<script>alert('请上传xlsx文件');window.location.href='goodslist.php';</script>";
	exit;
}
if (!@move_uploaded_file($_FILES['file']["tmp_name"], $_SERVER['DOCUMENT_ROOT'].$save_path.$name.".".$houzhui)) {	
	echo "<script>alert('error|上传文件错误');window.location.href='goodslist.php';</script>";
	exit;	
}

/*读取excel文件，并进行相应处理*/
$fileName = $_SERVER['DOCUMENT_ROOT'].$save_path.$name.".".$houzhui;
if (!file_exists($fileName)) {    
	echo "<script>alert(文件".$fileName."不存在);window.location.href='goodslist.php';</script>";
	exit;
}
$startTime = time(); //返回当前时间的Unix 时间戳
include($_SERVER['DOCUMENT_ROOT']."/inc/PHPExcel-1.8/Classes/PHPExcel/IOFactory.php");//引入PHPExcel类库
$ex = PHPExcel_IOFactory::load($fileName);
$ex->setActiveSheetIndex(0);//默认选中sheet0表
$sheet = $ex->getActiveSheet();
$xnum = $sheet->getHighestRow();//获取表格行数
$ynum = $sheet->getHighestColumn();//获取表格列数
$data = array();
//行数循环
for ($i=2;$i<=$xnum;$i++){
	$cplx = $sheet->getCell("B".$i)->getValue();		
	$name = $sheet->getCell("C".$i)->getValue();
	$tag1 = $sheet->getCell("D".$i)->getValue();
	$tag2 = $sheet->getCell("E".$i)->getValue();
	$gname = $sheet->getCell("F".$i)->getValue();
	$yprice = $sheet->getCell("G".$i)->getValue();
	$cash = $sheet->getCell("H".$i)->getValue();
	$kuaidif = $sheet->getCell("I".$i)->getValue();
	$gtotal = $sheet->getCell("J".$i)->getValue();
	$selltotal = $sheet->getCell("K".$i)->getValue();
	$px = $sheet->getCell("L".$i)->getValue();
	$data[] = array($cplx,$name,$tag1,$tag2,$gname,$yprice,$cash,$kuaidif,$gtotal,$selltotal,$px);
	if($cplx=='报单产品'){
		$cplx = 1;
	}else if($cplx=='置换产品'){
		$cplx = 2;
	}else if($cplx=='零售产品'){
		$cplx = 3;
	}
	$cinfo = $db->field(['*'])->where("name='".$name."'")->select("sc_catogry");
	if($cinfo){
		$cid = $cinfo[0]['Id'];
	}
	
	$tinfo = $db->field(['*'])->where("name='".$tag1."' and level=1")->select("sc_tag");
	if($tinfo){
		$tag1 = $tinfo[0]['Id'];
	}
	$tinfo = $db->field(['*'])->where("name='".$tag2."' and level=2")->select("sc_tag");
	if($tinfo){
		$tag2 = $tinfo[0]['Id'];
	}
	$db->insert("sc_goods",array("cplx"=>$cplx,"cid"=>$cid,"tag1"=>$tag1,"tag2"=>$tag2,"gname"=>$gname,"yprice"=>$yprice,"cash"=>$cash,"kuaidif"=>$kuaidif,"gtotal"=>$gtotal,"selltotal"=>$selltotal,"px"=>$px,"gstate"=>1));
}
echo "<script>alert('更新完成');window.location.href='goodslist.php';</script>";


?>